<?php

/**
 * This is the model class for table "item".
 *
 * The followings are the available columns in table 'item':
 * @property integer $item_id
 * @property integer $item_seller
 * @property string $item_name
 * @property double $item_price
 * @property string $item_description
 * @property string $item_image
 * @property string $item_filepath
 * @property integer $item_rating
 * @property string $item_tags
 * @property string $item_time
 *
 * The followings are the available model relations:
 * @property Comments[] $comments
 * @property User $itemSeller0
 * @property Purchase[] $purchases
 */
class Item extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Item the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'item';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('item_name, item_price, item_filepath, item_rating, item_time', 'required'),
			array('item_rating', 'numerical', 'integerOnly'=>true),
			array('item_price', 'numerical'),
			array('item_name', 'length', 'max'=>100),
			array('item_image, item_filepath', 'length', 'max'=>250),
			array('item_description, item_tags', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('item_id, item_seller, item_name, item_price, item_description, item_image, item_filepath, item_rating, item_tags, item_time', 'safe', 'on'=>'search'),
			array('item_filepath', 'file','allowEmpty'=>false,
			'types'=>'pdf,doc,ppt,xls',
			'maxSize'=>1024 * 1024 * 2, // 2MB
			'tooLarge'=>'The file was larger than 2MB. Please upload a smaller file.',
)
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'comments' => array(self::HAS_MANY, 'Comments', 'comm_item'),
			'itemSeller0' => array(self::BELONGS_TO, 'User', 'item_seller'),
			'purchases' => array(self::HAS_MANY, 'Purchase', 'purchase_item'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'item_id' => 'Item',
			'item_seller' => 'Item Seller',
			'item_name' => 'Note Name',
			'item_price' => 'Price',
			'item_description' => 'Description',
			'item_image' => 'Image File',
			'item_filepath' => 'Note File',
			'item_rating' => 'Rating',
			'item_tags' => 'Tags',
			'item_time' => 'Item Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('item_id',$this->item_id);
		$criteria->compare('item_seller',$this->item_seller);
		$criteria->compare('item_name',$this->item_name,true);
		$criteria->compare('item_price',$this->item_price);
		$criteria->compare('item_description',$this->item_description,true);
		$criteria->compare('item_image',$this->item_image,true);
		$criteria->compare('item_filepath',$this->item_filepath,true);
		$criteria->compare('item_rating',$this->item_rating);
		$criteria->compare('item_tags',$this->item_tags,true);
		$criteria->compare('item_time',$this->item_time,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}